package com.aaa.utils;

import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class AuthenticationUtils {

    @Resource
    TokenStore tokenStore;

    public  Map<String, Object> getAdditionalInformation(Authentication authentication){
        // 用户名
        Object details = authentication.getDetails();
        OAuth2AuthenticationDetails auth2AuthenticationDetails = (OAuth2AuthenticationDetails) details;
        // 获取token
        String tokenValue = auth2AuthenticationDetails.getTokenValue();
        // 解析token字符串
        OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue);
        Map<String, Object> map = accessToken.getAdditionalInformation();
        return map;
    }

    public String getUserId(Authentication authentication){
        return getAdditionalInformation(authentication).get("userid").toString();
    }
}
